<template>
  <div class="role-list">
    <el-card>
      <!-- 工具栏 -->
      <div class="toolbar">
        <el-button type="primary" @click="handleAdd">新增角色</el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="tableData"
      >
        <el-table-column prop="name" label="角色名称" width="150" />
        <el-table-column prop="code" label="角色编码" width="150" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" fixed="right" width="250">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="primary" @click="handlePermission(row)">权限设置</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增角色' : '编辑角色'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色编码" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 权限设置对话框 -->
    <el-dialog
      v-model="permissionVisible"
      title="权限设置"
      width="600px"
    >
      <el-tree
        ref="treeRef"
        :data="permissionTree"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        :default-checked-keys="checkedKeys"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="permissionVisible = false">取消</el-button>
          <el-button type="primary" @click="handlePermissionSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    id: 1,
    name: '管理员',
    code: 'ADMIN',
    description: '系统管理员，拥有所有权限',
    createTime: '2024-03-18 10:00:00'
  },
  {
    id: 2,
    name: '销售员',
    code: 'SALES',
    description: '销售人员，负责销售相关业务',
    createTime: '2024-03-18 10:00:00'
  },
  {
    id: 3,
    name: '仓管员',
    code: 'WAREHOUSE',
    description: '仓库管理员，负责库存管理',
    createTime: '2024-03-18 10:00:00'
  }
])

// 对话框
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref<FormInstance>()
const form = reactive({
  name: '',
  code: '',
  description: ''
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请���入角色名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入角色编码', trigger: 'blur' }]
}

// 权限树
const permissionVisible = ref(false)
const treeRef = ref()
const currentRole = ref<any>(null)
const checkedKeys = ref<number[]>([])
const permissionTree = ref([
  {
    id: 1,
    label: '系统管理',
    children: [
      { id: 11, label: '用户管理' },
      { id: 12, label: '角色管理' }
    ]
  },
  {
    id: 2,
    label: '药品管理',
    children: [
      { id: 21, label: '药品列表' },
      { id: 22, label: '药品分类' },
      { id: 23, label: '库存管理' }
    ]
  },
  {
    id: 3,
    label: '采购管理',
    children: [
      { id: 31, label: '采购订单' },
      { id: 32, label: '供应商管理' }
    ]
  },
  {
    id: 4,
    label: '销售管理',
    children: [
      { id: 41, label: '销售订单' },
      { id: 42, label: '销售记录' }
    ]
  }
])

const defaultProps = {
  children: 'children',
  label: 'label'
}

// 新增
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  // 重置表单
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
}

// 编辑
const handleEdit = (row: any) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  // 填充表单
  Object.keys(form).forEach(key => {
    form[key] = row[key]
  })
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(
    `确定要删除角色 ${row.name} 吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 实现删除逻辑
    ElMessage.success('删除成功')
  })
}

// 权限设置
const handlePermission = (row: any) => {
  currentRole.value = row
  permissionVisible.value = true
  // TODO: 获取角色权限并设置选中状态
  checkedKeys.value = [11, 21, 22]
}

// 表单提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
      dialogVisible.value = false
    }
  })
}

// 权限设置提交
const handlePermissionSubmit = () => {
  if (!treeRef.value) return
  const checkedNodes = treeRef.value.getCheckedNodes()
  const halfCheckedNodes = treeRef.value.getHalfCheckedNodes()
  // TODO: 实现权限保存逻辑
  console.log('checked:', checkedNodes)
  console.log('half checked:', halfCheckedNodes)
  ElMessage.success('权限设置成功')
  permissionVisible.value = false
}
</script>

<style scoped>
.role-list {
  padding: 20px;
}
.toolbar {
  margin-bottom: 20px;
}
</style> 